package com.CmJava.crowd.controller;


import com.CmJava.crowd.entity.ResultEntity;
import com.CmJava.crowd.entity.Email;
import com.CmJava.crowd.service.EmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
public class EmailController {

    @Autowired
    private EmailService emailService;

    //检查已经登陆邮箱了没
    @RequestMapping("checkFirstLoginEmail")
    @ResponseBody
    public ResultEntity checkFirstLoginEmail(HttpSession session){
        String sendHost1 = (String) session.getAttribute("sendHost");
        String password1 = (String) session.getAttribute("password");
        if(sendHost1==null||sendHost1==""||password1==null||password1==""){
            //session里面没有登录信息
            return ResultEntity.failWithoutMessage();
        }
        ////session里面有登录信息
        return ResultEntity.successWithData(password1);
    }

    /**
     *
     * @param sendHost 发送人邮箱
     * @param password 邮箱密码或授权码
     * @return
     */
    //登陆邮箱，底层是发送一份邮件
    @RequestMapping("loginEmail")
    @ResponseBody
    private ResultEntity loginEmail(@RequestParam("sendHost") String sendHost,
                                    @RequestParam("password") String password,
                                    HttpSession session){
        try {
            // 如果发件人邮箱和验证码有误，发送邮件失败会抛异常
            emailService.checkLoginEmail(sendHost, password);
        }catch (Exception exception){
            return ResultEntity.failWithoutMessage();
        }
        session.setAttribute("sendHost",sendHost);
        session.setAttribute("password",password);
        return ResultEntity.successWithoutData();
    }



    // 发送邮件(目前只实现了qq邮箱的发送)
    @RequestMapping("email")
    @ResponseBody
    public ResultEntity sendEmail(@RequestParam("recipientHost") String recipientHost ,
                                  @RequestParam("subject") String subject,
                                  @RequestParam("textMessage") String textMessage,
                                  @RequestParam("htmlMessage") String htmlMessage,
                                  @RequestParam("fileMessage") String fileMessage,
                                  @RequestParam("flag") int flag,
                                  @RequestParam("emailClass") String emailClass,
                                  HttpSession session
                                  ){
        String sendHost = (String) session.getAttribute("sendHost");
        String password = (String) session.getAttribute("password");
        if(sendHost==null||sendHost==""||password==null||password==""){
            //session里面没有登录信息
            return ResultEntity.failWithoutMessage();
        }
        // 获取标志位，这个应该在前端是checkbox的值返回的
        try {
            if (flag == 1) {
                // 发送文本邮件
                emailService.sendTextEmail(sendHost, password, recipientHost, subject,textMessage);
                return ResultEntity.successWithoutData();
            } else if (flag == 2) {
                //发送html邮件
                emailService.sendHtmlEmail(sendHost, password, recipientHost,subject, htmlMessage);
                return ResultEntity.successWithoutData();
            } else {
                // 发送文件邮件
                emailService.sendFileEmail(sendHost, password, recipientHost,subject, fileMessage);
                return ResultEntity.successWithoutData();
            }
        }catch(Exception e){
            return ResultEntity.failWithoutMessage();
        }
    }

    //获取邮件
    @RequestMapping("getEmail")
    @ResponseBody
    private ResultEntity getEmail(HttpSession session){
        // session有直接返回
        ResultEntity resultEntity  = null;
        if (session.getAttribute("emailList")!=null){
            resultEntity= (ResultEntity) session.getAttribute("emailList");
            return resultEntity;
        }
        String sendHost = (String) session.getAttribute("sendHost");
        String password = (String) session.getAttribute("password");
        resultEntity  = emailService.getEmail(sendHost,password);

        // 查询收件箱很慢的，查到第一次就会放在session里面；
        if (resultEntity.getResultFlag()==true){
            session.setAttribute("emailList",resultEntity);
        }
        return resultEntity;
    }


    // 获取单个邮件详情
    @RequestMapping("getEmailDetail")
    @ResponseBody
    private ResultEntity getEmailDetail(@RequestParam("address") String address,
                                        @RequestParam("subject") String subject,
                                        @RequestParam("date") String date,
                                        HttpSession session){
        if (session.getAttribute("emailList")!=null){
            ResultEntity emailListRE= (ResultEntity) session.getAttribute("emailList");
            Email data = (Email) emailListRE.getData();
            Email.emailUnit[] emailUnit = data.getEmailUnit();
            for (Email.emailUnit unit : emailUnit) {

                if(unit.getAddress().equals(address) && unit.getSubject().equals(subject) && unit.getDate().equals(date)){

                    return new ResultEntity<Email.emailUnit>(true,"",unit);
                }
            }
        }else {
            return ResultEntity.failWithoutMessage();
        }
        return ResultEntity.failWithoutMessage();
    }




    // 将邮件设为已读，ajax请求，不返回成功与否
    @RequestMapping("readEmailDetail")
    @ResponseBody
    private ResultEntity readEmailDetail(@RequestParam("address") String address,
                                        @RequestParam("subject") String subject,
                                        @RequestParam("date") String date,
                                        HttpSession session){

        String sendHost = (String) session.getAttribute("sendHost");
        String password = (String) session.getAttribute("password");
        // 在imap服务器把这个邮件已读
        ResultEntity resultEntity = emailService.readedThisEmail(sendHost,password,address,subject,date);

        // 已读成功，直接把session里面的那一封邮件置为已读
        if (resultEntity.getResultFlag()==true){
            ResultEntity emailListRE= (ResultEntity) session.getAttribute("emailList");
            Email emailList = (Email) emailListRE.getData();
            Email.emailUnit[] emailUnit = emailList.getEmailUnit();
            for (Email.emailUnit unit : emailUnit) {
                if(unit.getAddress().equals(address) && unit.getSubject().equals(subject) && unit.getDate().equals(date)){
                    unit.setUnread(1);
                }
            }
        }

        return resultEntity;
    }

}
